import random

from sqlalchemy import Table

from common.database import db_connect
db_session,Base,engine=db_connect()

class User(Base):
    # 表结构反射加载
    __table__ = Table('user', Base.metadata,autoload_with=engine)

    def __str__(self):
        return f"User(id={self.user_id}, username='{self.username}', email='{self.password}')"

    def get_one(self):
        return db_session.query(User).first()

    def find_by_username(self,username):
        #通过用户名
        return db_session.query(User).filter_by(username=username).all()
    #返回全部的user内容
    def do_register(self,username,password):
        nickname=username.split("@")[0]
        #头像
        picture_num=random.randint(1,20)
        picture=str(picture_num)+'.jpg'
        job="未定义"
        user=User(
            username=username,
            password=password,
            nickname=nickname,
            picture=picture,
            job=job,
        )
        print("user:",user.user_id,user.username)
        db_session.add(user)
        db_session.commit()
        return user